//
//  PhotoPicker.swift
//  aj
//
//  Created by leeqb on 1/29/16.
//  Copyright © 2016 powerlee. All rights reserved.
//

import UIKit

var g_photoPicker: PhotoPicker!

@objc protocol PhotoPickerDelegate {
    func photoPicked(image: UIImage, imagePath: String)
}

class PhotoPicker: UIView, UIImagePickerControllerDelegate, UINavigationControllerDelegate, UIActionSheetDelegate {
    var delegate: PhotoPickerDelegate!
    
    class func pick(delegate: PhotoPickerDelegate) {
        if g_photoPicker == nil {
            g_photoPicker = PhotoPicker()
        }
        
        g_photoPicker.delegate = delegate
        g_photoPicker.pick()
    }
    
    func pick() {
        let actionSheet = UIActionSheet(title: nil, delegate: self, cancelButtonTitle: "取消", destructiveButtonTitle: nil, otherButtonTitles: "从相册中选择", "拍照")
        actionSheet.showInView(UIApplication.sharedApplication().keyWindow!)
    }
    
    //MARK: - UIActionSheetDelegate
    func actionSheet(actionSheet: UIActionSheet, didDismissWithButtonIndex buttonIndex: Int) {
        if buttonIndex == 1 {
            let isPhotoAvailable = UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.PhotoLibrary)
            if isPhotoAvailable {
                let imagePicker = UIImagePickerController()
                imagePicker.delegate = self
                imagePicker.allowsEditing = true
                imagePicker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
                let rootVC = UIApplication.sharedApplication().keyWindow?.rootViewController
                rootVC?.presentViewController(imagePicker, animated: true, completion: { () -> Void in
                })
            }
        } else if buttonIndex == 2 {
            let isCameraAvailable = UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera)
            if isCameraAvailable {
                let imagePicker = UIImagePickerController()
                imagePicker.delegate = self
                imagePicker.allowsEditing = true
                imagePicker.sourceType = UIImagePickerControllerSourceType.Camera
                let rootVC = UIApplication.sharedApplication().keyWindow?.rootViewController
                rootVC?.presentViewController(imagePicker, animated: true, completion: { () -> Void in
                })
            }
        }
    }
    
    //MARK: - UIImagePickerControllerDelegate
    func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) {
        picker.dismissViewControllerAnimated(true) { () -> Void in
        }
        
        let params = ["smallWidth": 100, "smallHeigth": 100, "fileType": "png"]
        var finalParams = AJNetworkHelper.genRequestParams("aj_file_upload", parameters: params)
        finalParams["pic"] = image
        CBMessage.shareInstance().showProcessing()
        UploadHelper.uploadImage(finalParams, success: { (retData: [NSObject : AnyObject]!) -> Void in
            dispatch_async(dispatch_get_main_queue(), { () -> Void in
                CBMessage.shareInstance().dismiss()
                
                if self.delegate != nil {
                    let result = retData["result"] as? [NSObject : AnyObject]
                    if result != nil {
                        self.delegate.photoPicked(image, imagePath: result!["picPath"] as? String ?? "")
                    }
                }
            })
            }) { (error: NSError!) -> Void in
                dispatch_async(dispatch_get_main_queue(), { () -> Void in
                    CBMessage.shareInstance().dismiss()
                })
        }
    }
    
    func imagePickerControllerDidCancel(picker: UIImagePickerController) {
        picker.dismissViewControllerAnimated(true) { () -> Void in
        }
    }
}
